import random
from threading import Thread

import allure
import requests
import app


class Goods:
    def __init__(self):
        self.post_add_goods_url = "https"+app.BASE_URL + "/vega-server/cxfxGood/add"
        self.post_update_goods_url = "https"+app.BASE_URL + "/vega-server/cxfxGood/update"
        self.get_goods_list_url = "https" + app.BASE_URL + "/vega-server/cxfxGood/getGoodsList"
        requests.packages.urllib3.disable_warnings()
    # 添加商品
    @allure.step(title='添加商品')
    def post_add_goods(self,name,detail,weight,price):
        """
        :param name: 商品名称
        :param detail: 商品描述
        :param weight: 重量
        :param price: 价格
        :return:
        """
        myJson = {"name":name,"detail":detail,"weight":weight,"price":price}
        myHeads = {"Content-Type": "application/json","token":app.TOKEN}
        return requests.post(self.post_add_goods_url,json=myJson,headers=myHeads,verify=False)

    # 编辑商品
    @allure.step(title='编辑商品')
    def post_update_goods(self,id,name,detail,weight,price):
        myJson = {"id":id,"name":name,"detail":detail,"weight":weight,"price":price}
        myHeads = {"Content-Type": "application/json","token":app.TOKEN}
        return requests.post(self.post_update_goods_url,json=myJson,headers=myHeads,verify=False)

    # 获取商品列表
    @allure.step(title='获取商品列表')
    def get_goods_list(self):

        myData = {"pageSize":500,"pageNumber":1}
        myHeads = {"Content-Type": "application/x-www-form-urlencoded","token":app.TOKEN}
        return requests.get(self.get_goods_list_url,json=myData,headers=myHeads,verify=False)

if __name__ == '__main__':
    # a=Goods().get_goods_list().json()
    # print(a)
    # Goods().post_add_goods("香蕉" + str(random.randint(100, 1000)), "这是香蕉", 10, 0.1)
    def id109():
        print(Goods().get_goods_list().json()["msg"])
    def id108():
        print(Goods().get_goods_list().json())
    t1 = Thread(target=id108())
    t2 = Thread(target=id109())
    t1.start()
    t2.start()